/*
 * License:
 * =============================================================================
 * Copyright 2007  PANEL Martin
 * email : xxorax@gmail.com
 * website : http://www.xorax.info
 * 
 * This work is licensed under a Creative Commons Attribution-Share Alike 3.0
 * License (http://creativecommons.org/licenses/by-sa/3.0/)
*/
var globaleval =  function(script){
	if(window.execScript){
		return window.execScript(script);
	} else if(navigator.userAgent.indexOf('KHTML') != -1){ //safari, konqueror..
			var s = document.createElement('script');
			s.type = 'text/javascript';
			s.innerHTML = script;
			document.getElementsByTagName('head')[0].appendChild(s);
	} else {
		return window.eval(script);
	}
};

Ajax.savedForm = false;

Ajax.Responders.register({
	onCreate: function(th){
		if(Ajax.savedForm){
			th.options.savedForm = Ajax.savedForm;
		}
	},
	
  onComplete: function(th, xhr) {
		//$('info').innerHTML += 'complete<br/>';
		var oldActiveXHR = Ajax.activeRequestCount;
		
		if(th.options.savedForm){
			Ajax.savedForm = th.options.savedForm;
		}
		
   	try{
   	var fieldname = false;
   	$A(xhr.responseXML.getElementsByTagName('item')).each(function(item){
   		fieldname = false;
   		$A(item.childNodes).each(function(p){
   			if(!p.firstChild || !p.firstChild.nodeValue) return;
   			var c = p.firstChild;
   			if(!fieldname && p.tagName == 'field' && c.nodeType == 3){
   				fieldname = p.firstChild.nodeValue;
   			}else if(fieldname && p.nodeType == 1 && (c.nodeType==3 || c.nodeType==4)){
   				switch(p.tagName){
   				case 'head':
   				case 'foot':
   					Element.update(fieldname+'--'+p.tagName,p.firstChild.nodeValue);
   					new Effect.Highlight(fieldname+'--'+p.tagName,{duration: 1, startcolor:"#AA0300"});
   				break;
   				case 'reload':
   					Element.update(fieldname+'--content',p.firstChild.nodeValue);
   					new Effect.Highlight(fieldname+'--content',{duration: 1, startcolor:"#2685AF"});
   				break;
   				case 'eval':
   					globaleval(p.firstChild.nodeValue);
   				break;
   				}
   			}
   			
   		});
   	});
 		}catch(e){
 			alert('expection ajaxsend on complete:'+e);
 		}
 		
 		Ajax.savedForm = false;
 		
 		if(Ajax.activeRequestCount <= oldActiveXHR && th.options.savedForm){
 			var f = th.options.savedForm;
 			Form.enable(f.form);
 			var i = 0;
			Form.getInputs(f.form,'submit').each(function(s){ s.value = f.txt[i++]; });
		}
  },
  
  onException: function(th,e){
  	alert('Expection ajaxsend() : '+e);
  }
});

var add_wp_ajax_param = function (p){
	return p+'&action=readthis_xhr&cookie=' + encodeURIComponent(document.cookie);
};

var xorax_wp_ajax_callback = function (func,args,reload_field){ // args is serialized and base64 encoded
	p = 'func_callback='+func;
	if(args) p += '&func_callback_args='+encodeURIComponent(args);
	if(reload_field) p += '&reload_field='+reload_field;
	p = add_wp_ajax_param(p);
	new Ajax.Request('admin-ajax.php', {
		method: 'post',
		parameters: p
		}
	);
};

var posts_audio = [];

Form.ajaxsend= function(form) {
	var pa = Form.serialize(form, true);
	pa = add_wp_ajax_param(pa);
	Form.disable(form);
	var formtxt = [];
	Form.getInputs(form,'submit').each(function(s){formtxt.push(s.value); s.value = "Loading..."});
	new Ajax.Request(
		'admin-ajax.php',
		{
			method: form.method || 'post',
			savedForm: {form: form, txt: formtxt},
			parameters: pa
		}
	);
	return false;
};

Event.observe(window, 'load', function(){
	$A(document.getElementsByTagName('form')).each(function(v,k){
		//alert(Object.inspect(v));
		Event.observe(v, 'submit',function(){return Form.ajaxsend(v);} );
		v.onsubmit = function(){return false;}
	});
});
